<?php
/**
 * try to connect a MySQL server and login to RAIL.
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation. Either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. Without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. Ff not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * Copyright (C) 2006-2007
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2008-04-10
 * @package railadmin
 */

function ajax_call( $ajaxBridge ) {
	try{
		// connect to MySQL
		$mysqli = new mysqli( $_POST['mysql_host'], $_POST['mysql_user'], $_POST['mysql_passwd'] );
		if( mysqli_connect_errno() )
			throw new Exception( mysqli_connect_error() );

		$_SESSION['railadmin_MYSQL_host'] = $_POST["mysql_host"];
		$_SESSION['railadmin_MYSQL_user'] = $_POST["mysql_user"];
		$_SESSION['railadmin_MYSQL_passwd'] = $_POST["mysql_passwd"];
		$ajaxBridge->append( 'connect_mysql', true );

		// connect to rail
		unset( $_SESSION['railadmin_MYSQL_db'] );
		if( isset( $_POST['mysql_db'] ) && strlen( $_POST['mysql_db'] ) > 0 ){
			new rail_connector( $_POST['mysql_db'], $_POST['mysql_user'], $_POST['mysql_passwd'], $_POST['mysql_host']);
			// set session vars
			unset( $_SESSION['railadmin_railuser'] );
			unset( $_SESSION['railadmin_railpasswd'] );
			$_SESSION['railadmin_MYSQL_db'] = $_POST['mysql_db'];
			$_SESSION['railadmin_connected'] = true;
			$ajaxBridge->append( 'connect_rail', true );
		}
	}
	catch( Exception $e ){
		$ajaxBridge->register_exception( $e, 3 );
	}
}

?>